package com.swipemaster.achievements;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.app.Activity;
import android.content.Context;
import android.widget.TextView;

import com.swipemaster.R;

/**
 * Gives public access to achievements and allow basic operations on them. 
 * You can't create an instance of manager, you should just call its static methods.
 */
abstract public class AchievementsManager
{
	private static Achievements achievements;

	public static void updateTextViews(Activity activity)
	{
		getAchievements(activity);
		TextView textView;
		textView = (TextView) activity.findViewById(R.id.games_played);
		textView.setText(achievements.getGamesPlayed() + "");
		textView = (TextView) activity.findViewById(R.id.stars);
		textView.setText(achievements.getStars() + "");
		textView = (TextView) activity.findViewById(R.id.challenge_record);
		textView.setText(achievements.getChallengeRecord() + "");
		textView = (TextView) activity.findViewById(R.id.survival_record);
		textView.setText(achievements.getSurvivalRecord() + "s");
		textView = (TextView) activity.findViewById(R.id.avg_time_from_puzzle_records);
		textView.setText(achievements.getAvgTimeFromPuzzleRecords() + "s");
		textView = (TextView) activity.findViewById(R.id.avg_time_from_survival_records);
		textView.setText(achievements.getAvgTimeFromSurvivalRecords() + "s");
	}

	public static void saveAchievements(Activity activity)
	{
		getAchievements(activity);
		try
		{
			FileOutputStream fos = activity.openFileOutput(activity.getString(R.string.achievements), Context.MODE_PRIVATE);
			ObjectOutputStream out = new ObjectOutputStream(fos);
			out.writeObject(achievements);
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * First, try to load achievements from file. If no such file exists, than create new achievements.
	 */
	private static void createOrLoadAchievements(Activity activity)
	{
		ObjectInputStream in = null;
		try
		{
			FileInputStream fis = activity.openFileInput(activity.getString(R.string.achievements)); 
			in = new ObjectInputStream(fis);
		}
		catch (IOException e)
		{
			achievements = new Achievements();
			return;
		}
		loadAchievements(in);
	}
	
	/**
	 * Load achievements from given stream.
	 */
	private static void loadAchievements(ObjectInputStream in)
	{
		try
		{
			achievements = (Achievements) in.readObject();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		try
		{
			in.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Retrieves achievements, they will be loaded or created.
	 */
	public static Achievements getAchievements(Activity activity)
	{
		if (achievements == null)
		{
			createOrLoadAchievements(activity);
		}
		return achievements;
	}
	
	public static LevelsInfo getLevelsInfo(Activity activity)
	{
		return getAchievements(activity).getLevelsInfo();
	}
}
